package com.proxy.dynamic_proxy_jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 项目名称: java_algorithm
 * @ClassName AgentDynamicProxy
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2022/5/27 16:46
 */
public class AgentDynamicProxy implements InvocationHandler {

    private Rent rent;

    public void setRent(Rent rent) {
        this.rent = rent;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //前置增强
        System.out.println("筹钱准备买房");
        Object returnVal= method.invoke(rent, args);
        //后置增强
        System.out.println("开心入住");
        return returnVal;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(AgentDynamicProxy.class.getClassLoader(),new Class[]{Rent.class},this);
    }
}
